'''
Created on Aug 27, 2010

@author: alia
'''

import logging
log = logging.getLogger('mouse')
log.debug("imported")
 
import cocos
from cocos import draw
from cocos.euclid import *
from cocos.actions import *
from cocos.director import director

import pyglet

log.debug("imports finished")


           
class MouseManager(cocos.layer.Layer):
    is_event_handler = True
    MAX_QUEUE_LENGTH = 5
    
    def __init__(self, player, scroller):
        super(MouseManager, self).__init__()
        self.player = player
        self.player.mouse = self
        
        self.scroller = scroller
        self.mouse = Vector2(0,0)
        self.screen_pos = (0,0)
        self.schedule(self.update)
        self.mouse_queue = []
        self.lines = []
        self.last_line = None
        self.draw_target = self

    def get_next_mouse(self):
        log.debug("get_next_mouse")
        self.screen_pos = self.mouse_queue.pop(0) 
        line = self.lines.pop(0)
        
        start = line.start
        end = line.end
        
        if line is not None:
            line.do(FadeOut(1)+CallFunc(self.draw_target.remove, line))
            
        return start,end
            
    def update_path(self):
        log.debug("update_path")
        line = self.player.path.pop(0)
        
        start = line.start
        end = line.end
        
        if line is not None:
            line.do(FadeOut(0.3)+CallFunc(self.draw_target.remove, line))
            
        return start,end

    def _convert_coord(self, xy):
        if self.scroller is not None:
            x, y = self.scroller.pixel_from_screen(xy[0], xy[1])
        else:
            x, y = director.get_virtual_coordinates(xy[0], xy[1]) 
        return Vector2(x, y)
            
    def update(self, dt):
        pass

        
    def on_mouse_press(self, x, y, buttons, modifiers):
        log.debug( "MouseManager on_mouse_press:"+str( (x, y, buttons, modifiers)))
 
        #x, y = self.parent.parent.pixel_to_screen(x, y)
        self.player.click(x, y)
        
    def on_mouse_motion(self, x, y, dx, dy):
        #self.screen_pos = (x,y)
        if len(self.player.path) > self.MAX_QUEUE_LENGTH:
            log.debug( "MouseManager queue too long - poping off last "+ str(len(self.player.path)))
            #self.mouse_queue.pop()
            start, end = self.update_path()
            log.debug( "MouseManager queue too long - last "+ str((start, end)))
            
        #self.mouse_queue.append((x,y))
        
        if self.last_line is None:
            self.last_line = self._convert_coord((x,y))
        
        
            
        start = self.last_line#self._convert_coord(self.mouse_queue[-1])#
        end = self._convert_coord((x,y))#elf._convert_coord(self.mouse_queue[-2])#
        #if (end-self.player.pos).magnitude() >200:  
        #     self.last_line = end
        #     return
             
        if (start-end).magnitude() > self.player.PATH_INTERVAL: 
            line = draw.Line(start, end, (255,0,0,100), self.player.PATH_RADIUS)
            self.draw_target.add( line )     
            self.player.path.append(line) 
                
            self.last_line = end
                
            log.debug( "MouseManager added to queue "+ str((start, end, len(self.player.path))))



